package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.UacDept;
import com.csun.cmny.provider.model.dto.dept.UacDeptCheckNameDto;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.UacDeptService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2018-12-13 09:37
 * @Description: 部门
 */
@RestController
@RequestMapping(value = "/dept", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UacDeptCommonController extends BaseController {

    @Resource
    private UacDeptService uacDeptService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @PostMapping(value = "/checkDeptName")
    @ApiOperation(httpMethod = "POST", value = "检测部门名称唯一性")
    public Wrapper<Boolean> checkUacDeptName(@RequestBody UacDeptCheckNameDto uacDeptCheckNameDto) {

        Long id = uacDeptCheckNameDto.getDeptId();
        Long pid = uacDeptCheckNameDto.getPid();
        Long groupId = getLoginAuthDto().getGroupId();
        String deptName = uacDeptCheckNameDto.getDeptName();

        Example example = new Example(UacDept.class);
        Example.Criteria criteria = example.createCriteria();

        if (id != null) {
            criteria.andNotEqualTo("id", id);
        }

        criteria.andEqualTo("deptName", deptName);
        criteria.andEqualTo("pid", pid);
        criteria.andEqualTo("groupId", groupId);

        int result = uacDeptService.selectCountByExample(example);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result<1);
    }

}
